package com.egao.api.controller;


import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.orderB.entity.Address;
import com.egao.orderB.entity.Bill;
import com.egao.orderB.entity.BillImport;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.service.AddressService;
import com.egao.orderB.service.BillService;
import com.egao.orderB.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 * http://192.168.1.110/swagger-ui.html
 */
@RequestMapping("/api/customerBill")
@RestController
@Api(value = "货代下单接口Api",tags = {"货代下单接口Api"})
public class ApiCustomerBillController extends BaseController {

    @Autowired
    private BillService billService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private AddressService addressService;

    @PreAuthorize("hasAuthority('orderB:bill:offer')")
    @PostMapping("/customerBillAdd")
    @ApiOperation(value = "货代下单 ",notes = "" +
            "dock:码头A 必填\n" +
            "addressId:地址id\n" +
            "addressType:类型\n" +
            "weight:重量 必填\n" +
            "boxType:箱型 必填\n" +
            "packDate:装箱日期(货代要求的装箱时间) 必填\n" +
            "vessel:船名 必填\n" +
            "voyage:航次 必填\n" +
            "billOfLading:提单号 必填\n" +
            "factoryAddress:工厂地址 必填\n" +
            "factoryPhone:装箱人电话\n" +
            "annexFile:附件(上传的具体文件)\n" +
            "remark:备注\n" +
            "",httpMethod = "POST")
    public JsonResult customerBillAdd(BillImport billImport, HttpServletRequest request) {
        try{
            Customer customer = customerService.getById(new Customer(getLoginUser().getCustomerId()+"", true));

            Bill bill = new Bill();
            bill.setCustomerId(customer.getId());
            bill.setCustomer(customer.getName());
            return billService.BillOrder(billImport, bill, getLoginUser());
        } catch (IndexOutOfBoundsException e) {
            return JsonResult.error("没有客户");
        }
    }

    /**
     * 查找货代运费报价
     * 如果没有找到就去maintainFee表里去找
     */
    @PreAuthorize("hasAuthority('orderB:address:list')")
    @PostMapping("/searchAddress")
    @ApiOperation(value = "查找货代运费报价",notes = "" +
            "customerName:地址(模糊搜索) 必填\n" +
            "dock:码头 必填\n" +
            "返回的字段:\n" +
            "id:id\n" +
            "adressType:表类型\n" +
            "address:详细地址\n" +
            "boxType:箱型\n" +
            "price:价格\n" +
            "",httpMethod = "POST")
    public JsonResult searchAddress(Address address, HttpServletRequest request)  throws Exception{
        address.setCustomerId(getLoginUser().getCustomerId()+"");
        return addressService.searchAddressFee2(address, getLoginUser());
    }

}
